package com.jic.product.impl;

import java.lang.Long;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductMedia;
import com.jic.product.manage.request.ProductMediaRequest;
import com.jic.product.mapper.ProductMediaMapper;
import com.jic.product.service.ProductMediaService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import com.jic.product.rest.response.ProductMediaResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品媒体表
 * @author : 
 * @email : 
 * @since : 2020-01-10 07:03:01
 * @version : v1.0.0
 */
@Service
@Slf4j
public class ProductMediaServiceImpl implements ProductMediaService{

    @Autowired
    private ProductMediaMapper productMediaMapper;

    @Override
    public long insert(ProductMedia productMedia) {
        long flag = productMediaMapper.insert(productMedia);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productMediaMapper.delete(id);
    }

    @Override
    public long update(ProductMedia productMedia) {
        return productMediaMapper.update(productMedia);
    }

    @Override
    public ProductMedia queryByPrimaryKey(Long id){
        return productMediaMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductMedia> query(ProductMedia productMedia){
        return productMediaMapper.query(productMedia);
    }

    @Override
    public List<JSONObject> queryFW(Long productId) {
        return productMediaMapper.queryFWList(productId);
    }

    @Override
    public PageResult<ProductMedia> queryPage(ProductMedia productMedia, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductMedia> list = productMediaMapper.queryPage(startIndex, pageQuery.getLimit(), productMedia);
        long count = productMediaMapper.queryPageCount(productMedia);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    //根据商品id查询商品媒介
    @Override
    public List<ProductMediaResponse> queryByProductId(long id){
        return productMediaMapper.queryByProductId(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult saveProductMedia(ProductMediaRequest productMediaRequest) {
        //先删除已经保存的
        RestResult restResult = new RestResult();
        log.info("开始进入商品图片保存=======》"+productMediaRequest.toString());
        try {
            productMediaMapper.delete(productMediaRequest.getProductMediaList().get(0).getProductId());
            //保存图片
            productMediaRequest.getProductMediaList().forEach(item->{
                ProductMedia productMedia=new ProductMedia();
                productMedia= BeanUtil.copy(item,ProductMedia.class);
                productMediaMapper.insert(productMedia);
            });
            productMediaMapper.delFenWei(productMediaRequest.getProductId());
            JSONObject param = new JSONObject();
            param.put("productId",productMediaRequest.getProductId());
            param.put("validStartTime",productMediaRequest.getValidStartTime());
            param.put("validEndTime",productMediaRequest.getValidEndTime());
            //保存氛围图
            productMediaRequest.getProductFWList().forEach(item->{
                param.put("url",item.getPictureUrl());
                productMediaMapper.insertFenWei(param);
            });
            return RestResult.success(true);
        } catch (Exception e) {
            log.error("insert======>"+e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }

}